{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Train Dots and Boxes using AlphaZero General.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "machine_shape": "hm"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "HkvJ-kBlWXHW"
      },
      "source": [
        "# **Setup, install dependencies**"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Ji7OkVsnExHk",
        "outputId": "82195c00-2ebd-46e3-9c58-e15e7fd6156b",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "# Clone repo and install requirements\n",
        "\n",
        "!git clone https://github.com/suragnair/alpha-zero-general.git"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Cloning into 'alpha-zero-general'...\n",
            "remote: Enumerating objects: 1045, done.\u001b[K\n",
            "remote: Total 1045 (delta 0), reused 0 (delta 0), pack-reused 1045\u001b[K\n",
            "Receiving objects: 100% (1045/1045), 423.63 MiB | 24.50 MiB/s, done.\n",
            "Resolving deltas: 100% (575/575), done.\n",
            "Checking out files: 100% (109/109), done.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SadDZdC2lQXS",
        "outputId": "15ee85bd-4d36-4933-f798-246bceb63d3c",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "%cd '/content/alpha-zero-general'"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/content/alpha-zero-general\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AvtLNTQTWl4c",
        "outputId": "8cc0c261-e36e-40d1-b416-bcd677dd5905",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "!git checkout -t origin/master"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Branch 'dotsandboxes' set up to track remote branch 'dotsandboxes' from 'origin'.\n",
            "Switched to a new branch 'dotsandboxes'\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "w1483UJ1lS3J",
        "outputId": "c89ffe8b-f4ae-4284-ca39-f81c08eae4b2",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        }
      },
      "source": [
        "!pip install -r docker/requirements.txt"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Collecting cffi==1.11.5\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/6d/c0/47db8f624f3e4e2f3f27be03a93379d1ba16a1450a7b1aacfa0366e2c0dd/cffi-1.11.5-cp36-cp36m-manylinux1_x86_64.whl (421kB)\n",
            "\u001b[K     |████████████████████████████████| 430kB 8.7MB/s \n",
            "\u001b[?25hCollecting coloredlogs==14.0\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/5c/2f/12747be360d6dea432e7b5dfae3419132cb008535cfe614af73b9ce2643b/coloredlogs-14.0-py2.py3-none-any.whl (43kB)\n",
            "\u001b[K     |████████████████████████████████| 51kB 8.4MB/s \n",
            "\u001b[?25hCollecting cython==0.28.3\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/6f/79/d8e2cd00bea8156a995fb284ce7b6677c49eccd2d318f73e201a9ce560dc/Cython-0.28.3-cp36-cp36m-manylinux1_x86_64.whl (3.4MB)\n",
            "\u001b[K     |████████████████████████████████| 3.4MB 19.0MB/s \n",
            "\u001b[?25hCollecting flask==1.0\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/55/b1/4365193655df97227ace49311365cc296e74b60c7f5c63d23cd30175e2f6/Flask-1.0-py2.py3-none-any.whl (97kB)\n",
            "\u001b[K     |████████████████████████████████| 102kB 13.2MB/s \n",
            "\u001b[?25hCollecting gitpython==2.1.11\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/fe/e5/fafe827507644c32d6dc553a1c435cdf882e0c28918a5bab29f7fbebfb70/GitPython-2.1.11-py2.py3-none-any.whl (448kB)\n",
            "\u001b[K     |████████████████████████████████| 450kB 64.9MB/s \n",
            "\u001b[?25hCollecting matplotlib==2.1.1\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/34/50/d1649dafaecc91e360b1ca8defebb25f865e29928a98bc7d42ba3b1350e5/matplotlib-2.1.1-cp36-cp36m-manylinux1_x86_64.whl (15.0MB)\n",
            "\u001b[K     |████████████████████████████████| 15.0MB 113kB/s \n",
            "\u001b[?25hCollecting numpy==1.14.5\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/68/1e/116ad560de97694e2d0c1843a7a0075cc9f49e922454d32f49a80eb6f1f2/numpy-1.14.5-cp36-cp36m-manylinux1_x86_64.whl (12.2MB)\n",
            "\u001b[K     |████████████████████████████████| 12.2MB 14.0MB/s \n",
            "\u001b[?25hCollecting pandas==0.23.1\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/57/eb/6ab533ea8e35e7dd159af6922ac1123d4565d89f3926ad9a6aa46530978f/pandas-0.23.1-cp36-cp36m-manylinux1_x86_64.whl (11.8MB)\n",
            "\u001b[K     |████████████████████████████████| 11.8MB 60.0MB/s \n",
            "\u001b[?25hCollecting scipy==1.1.0\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/a8/0b/f163da98d3a01b3e0ef1cab8dd2123c34aee2bafbb1c5bffa354cc8a1730/scipy-1.1.0-cp36-cp36m-manylinux1_x86_64.whl (31.2MB)\n",
            "\u001b[K     |████████████████████████████████| 31.2MB 80kB/s \n",
            "\u001b[?25hCollecting scikit-learn==0.19.1\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/3d/2d/9fbc7baa5f44bc9e88ffb7ed32721b879bfa416573e85031e16f52569bc9/scikit_learn-0.19.1-cp36-cp36m-manylinux1_x86_64.whl (12.4MB)\n",
            "\u001b[K     |████████████████████████████████| 12.4MB 49.9MB/s \n",
            "\u001b[?25hCollecting scikit-image==0.14.0\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/34/79/cefff573a53ca3fb4c390739d19541b95f371e24d2990aed4cd8837971f0/scikit_image-0.14.0-cp36-cp36m-manylinux1_x86_64.whl (25.3MB)\n",
            "\u001b[K     |████████████████████████████████| 25.3MB 130kB/s \n",
            "\u001b[?25hCollecting torchfile==0.1.0\n",
            "  Downloading https://files.pythonhosted.org/packages/91/af/5b305f86f2d218091af657ddb53f984ecbd9518ca9fe8ef4103a007252c9/torchfile-0.1.0.tar.gz\n",
            "Collecting torchvision==0.2.1\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/ca/0d/f00b2885711e08bd71242ebe7b96561e6f6d01fdb4b9dcf4d37e2e13c5e1/torchvision-0.2.1-py2.py3-none-any.whl (54kB)\n",
            "\u001b[K     |████████████████████████████████| 61kB 9.4MB/s \n",
            "\u001b[?25hCollecting tqdm==4.19.5\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/71/3c/341b4fa23cb3abc335207dba057c790f3bb329f6757e1fcd5d347bcf8308/tqdm-4.19.5-py2.py3-none-any.whl (51kB)\n",
            "\u001b[K     |████████████████████████████████| 61kB 9.0MB/s \n",
            "\u001b[?25hCollecting visdom==0.1.7\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/0e/f2/27b5d7c34b718afb355587d4e0c1f9108e925db4c0c932e935ba01051efd/visdom-0.1.7.tar.gz (43kB)\n",
            "\u001b[K     |████████████████████████████████| 51kB 7.8MB/s \n",
            "\u001b[?25hRequirement already satisfied: pycparser in /usr/local/lib/python3.6/dist-packages (from cffi==1.11.5->-r docker/requirements.txt (line 1)) (2.20)\n",
            "Collecting humanfriendly>=7.1\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/8e/2d/2f1b0a780b8c948c06c74c8c80e68ac354da52397ba432a1c5ac1923c3af/humanfriendly-8.2-py2.py3-none-any.whl (86kB)\n",
            "\u001b[K     |████████████████████████████████| 92kB 11.7MB/s \n",
            "\u001b[?25hRequirement already satisfied: itsdangerous>=0.24 in /usr/local/lib/python3.6/dist-packages (from flask==1.0->-r docker/requirements.txt (line 4)) (1.1.0)\n",
            "Requirement already satisfied: Werkzeug>=0.14 in /usr/local/lib/python3.6/dist-packages (from flask==1.0->-r docker/requirements.txt (line 4)) (1.0.1)\n",
            "Requirement already satisfied: click>=5.1 in /usr/local/lib/python3.6/dist-packages (from flask==1.0->-r docker/requirements.txt (line 4)) (7.1.2)\n",
            "Requirement already satisfied: Jinja2>=2.10 in /usr/local/lib/python3.6/dist-packages (from flask==1.0->-r docker/requirements.txt (line 4)) (2.11.2)\n",
            "Collecting gitdb2>=2.0.0\n",
            "  Downloading https://files.pythonhosted.org/packages/52/7e/59f96b47f671b3fe0aa0c1b609531a540434b719a10c417581e25b383909/gitdb2-4.0.2-py3-none-any.whl\n",
            "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib==2.1.1->-r docker/requirements.txt (line 6)) (2.4.7)\n",
            "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib==2.1.1->-r docker/requirements.txt (line 6)) (0.10.0)\n",
            "Requirement already satisfied: pytz in /usr/local/lib/python3.6/dist-packages (from matplotlib==2.1.1->-r docker/requirements.txt (line 6)) (2018.9)\n",
            "Requirement already satisfied: python-dateutil>=2.0 in /usr/local/lib/python3.6/dist-packages (from matplotlib==2.1.1->-r docker/requirements.txt (line 6)) (2.8.1)\n",
            "Requirement already satisfied: six>=1.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib==2.1.1->-r docker/requirements.txt (line 6)) (1.15.0)\n",
            "Requirement already satisfied: cloudpickle>=0.2.1 in /usr/local/lib/python3.6/dist-packages (from scikit-image==0.14.0->-r docker/requirements.txt (line 11)) (1.3.0)\n",
            "Requirement already satisfied: networkx>=1.8 in /usr/local/lib/python3.6/dist-packages (from scikit-image==0.14.0->-r docker/requirements.txt (line 11)) (2.5)\n",
            "Requirement already satisfied: pillow>=4.3.0 in /usr/local/lib/python3.6/dist-packages (from scikit-image==0.14.0->-r docker/requirements.txt (line 11)) (7.0.0)\n",
            "Requirement already satisfied: PyWavelets>=0.4.0 in /usr/local/lib/python3.6/dist-packages (from scikit-image==0.14.0->-r docker/requirements.txt (line 11)) (1.1.1)\n",
            "Requirement already satisfied: dask[array]>=0.9.0 in /usr/local/lib/python3.6/dist-packages (from scikit-image==0.14.0->-r docker/requirements.txt (line 11)) (2.12.0)\n",
            "Requirement already satisfied: torch in /usr/local/lib/python3.6/dist-packages (from torchvision==0.2.1->-r docker/requirements.txt (line 13)) (1.7.0+cu101)\n",
            "Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from visdom==0.1.7->-r docker/requirements.txt (line 15)) (2.23.0)\n",
            "Requirement already satisfied: tornado in /usr/local/lib/python3.6/dist-packages (from visdom==0.1.7->-r docker/requirements.txt (line 15)) (5.1.1)\n",
            "Requirement already satisfied: pyzmq in /usr/local/lib/python3.6/dist-packages (from visdom==0.1.7->-r docker/requirements.txt (line 15)) (19.0.2)\n",
            "Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.6/dist-packages (from Jinja2>=2.10->flask==1.0->-r docker/requirements.txt (line 4)) (1.1.1)\n",
            "Collecting gitdb>=4.0.1\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/48/11/d1800bca0a3bae820b84b7d813ad1eff15a48a64caea9c823fc8c1b119e8/gitdb-4.0.5-py3-none-any.whl (63kB)\n",
            "\u001b[K     |████████████████████████████████| 71kB 10.4MB/s \n",
            "\u001b[?25hRequirement already satisfied: decorator>=4.3.0 in /usr/local/lib/python3.6/dist-packages (from networkx>=1.8->scikit-image==0.14.0->-r docker/requirements.txt (line 11)) (4.4.2)\n",
            "Requirement already satisfied: toolz>=0.7.3; extra == \"array\" in /usr/local/lib/python3.6/dist-packages (from dask[array]>=0.9.0->scikit-image==0.14.0->-r docker/requirements.txt (line 11)) (0.11.1)\n",
            "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.6/dist-packages (from torch->torchvision==0.2.1->-r docker/requirements.txt (line 13)) (3.7.4.3)\n",
            "Requirement already satisfied: dataclasses in /usr/local/lib/python3.6/dist-packages (from torch->torchvision==0.2.1->-r docker/requirements.txt (line 13)) (0.7)\n",
            "Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from torch->torchvision==0.2.1->-r docker/requirements.txt (line 13)) (0.16.0)\n",
            "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests->visdom==0.1.7->-r docker/requirements.txt (line 15)) (2.10)\n",
            "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests->visdom==0.1.7->-r docker/requirements.txt (line 15)) (3.0.4)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests->visdom==0.1.7->-r docker/requirements.txt (line 15)) (2020.6.20)\n",
            "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests->visdom==0.1.7->-r docker/requirements.txt (line 15)) (1.24.3)\n",
            "Collecting smmap<4,>=3.0.1\n",
            "  Downloading https://files.pythonhosted.org/packages/b0/9a/4d409a6234eb940e6a78dfdfc66156e7522262f5f2fecca07dc55915952d/smmap-3.0.4-py2.py3-none-any.whl\n",
            "Building wheels for collected packages: torchfile, visdom\n",
            "  Building wheel for torchfile (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for torchfile: filename=torchfile-0.1.0-cp36-none-any.whl size=5711 sha256=87717a63bc2a4a0d7ba29ad4b5a822a0d4dd5960c83e901548b8cf57b361f01e\n",
            "  Stored in directory: /root/.cache/pip/wheels/b1/c3/d6/9a1cc8f3a99a0fc1124cae20153f36af59a6e683daca0a0814\n",
            "  Building wheel for visdom (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for visdom: filename=visdom-0.1.7-cp36-none-any.whl size=34498 sha256=4ee1bcf95c8327018b925cc85a66a88530b4fa3edf275f7c6cac88d515b4365a\n",
            "  Stored in directory: /root/.cache/pip/wheels/5c/ea/0f/4e3a4b23f352c708d36d3a7ff654fb014f893f50baa88f2d29\n",
            "Successfully built torchfile visdom\n",
            "\u001b[31mERROR: yellowbrick 0.9.1 has requirement scikit-learn>=0.20, but you'll have scikit-learn 0.19.1 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: xarray 0.15.1 has requirement numpy>=1.15, but you'll have numpy 1.14.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: xarray 0.15.1 has requirement pandas>=0.25, but you'll have pandas 0.23.1 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: umap-learn 0.4.6 has requirement numpy>=1.17, but you'll have numpy 1.14.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: umap-learn 0.4.6 has requirement scikit-learn>=0.20, but you'll have scikit-learn 0.19.1 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: umap-learn 0.4.6 has requirement scipy>=1.3.1, but you'll have scipy 1.1.0 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: tifffile 2020.9.3 has requirement numpy>=1.15.1, but you'll have numpy 1.14.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: tensorflow 2.3.0 has requirement numpy<1.19.0,>=1.16.0, but you'll have numpy 1.14.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: tensorflow 2.3.0 has requirement scipy==1.4.1, but you'll have scipy 1.1.0 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: spacy 2.2.4 has requirement numpy>=1.15.0, but you'll have numpy 1.14.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: spacy 2.2.4 has requirement tqdm<5.0.0,>=4.38.0, but you'll have tqdm 4.19.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: seaborn 0.11.0 has requirement matplotlib>=2.2, but you'll have matplotlib 2.1.1 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: seaborn 0.11.0 has requirement numpy>=1.15, but you'll have numpy 1.14.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: rpy2 3.2.7 has requirement cffi>=1.13.1, but you'll have cffi 1.11.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: plotnine 0.6.0 has requirement matplotlib>=3.1.1, but you'll have matplotlib 2.1.1 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: plotnine 0.6.0 has requirement numpy>=1.16.0, but you'll have numpy 1.14.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: plotnine 0.6.0 has requirement pandas>=0.25.0, but you'll have pandas 0.23.1 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: plotnine 0.6.0 has requirement scipy>=1.2.0, but you'll have scipy 1.1.0 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: numba 0.48.0 has requirement numpy>=1.15, but you'll have numpy 1.14.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: mizani 0.6.0 has requirement matplotlib>=3.1.1, but you'll have matplotlib 2.1.1 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: mizani 0.6.0 has requirement pandas>=0.25.0, but you'll have pandas 0.23.1 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: imgaug 0.2.9 has requirement numpy>=1.15.0, but you'll have numpy 1.14.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: imbalanced-learn 0.4.3 has requirement scikit-learn>=0.20, but you'll have scikit-learn 0.19.1 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: google-colab 1.0.0 has requirement pandas~=1.1.0; python_version >= \"3.0\", but you'll have pandas 0.23.1 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: fbprophet 0.7.1 has requirement numpy>=1.15.4, but you'll have numpy 1.14.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: fbprophet 0.7.1 has requirement pandas>=1.0.4, but you'll have pandas 0.23.1 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: fbprophet 0.7.1 has requirement tqdm>=4.36.1, but you'll have tqdm 4.19.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: fastai 1.0.61 has requirement numpy>=1.15, but you'll have numpy 1.14.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: datascience 0.10.6 has requirement folium==0.2.1, but you'll have folium 0.8.3 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: cvxpy 1.0.31 has requirement numpy>=1.15, but you'll have numpy 1.14.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: blis 0.4.1 has requirement numpy>=1.15.0, but you'll have numpy 1.14.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: astropy 4.1 has requirement numpy>=1.16, but you'll have numpy 1.14.5 which is incompatible.\u001b[0m\n",
            "\u001b[31mERROR: albumentations 0.1.12 has requirement imgaug<0.2.7,>=0.2.5, but you'll have imgaug 0.2.9 which is incompatible.\u001b[0m\n",
            "Installing collected packages: cffi, humanfriendly, coloredlogs, cython, flask, smmap, gitdb, gitdb2, gitpython, numpy, matplotlib, pandas, scipy, scikit-learn, scikit-image, torchfile, torchvision, tqdm, visdom\n",
            "  Found existing installation: cffi 1.14.3\n",
            "    Uninstalling cffi-1.14.3:\n",
            "      Successfully uninstalled cffi-1.14.3\n",
            "  Found existing installation: Cython 0.29.21\n",
            "    Uninstalling Cython-0.29.21:\n",
            "      Successfully uninstalled Cython-0.29.21\n",
            "  Found existing installation: Flask 1.1.2\n",
            "    Uninstalling Flask-1.1.2:\n",
            "      Successfully uninstalled Flask-1.1.2\n",
            "  Found existing installation: numpy 1.18.5\n",
            "    Uninstalling numpy-1.18.5:\n",
            "      Successfully uninstalled numpy-1.18.5\n",
            "  Found existing installation: matplotlib 3.2.2\n",
            "    Uninstalling matplotlib-3.2.2:\n",
            "      Successfully uninstalled matplotlib-3.2.2\n",
            "  Found existing installation: pandas 1.1.4\n",
            "    Uninstalling pandas-1.1.4:\n",
            "      Successfully uninstalled pandas-1.1.4\n",
            "  Found existing installation: scipy 1.4.1\n",
            "    Uninstalling scipy-1.4.1:\n",
            "      Successfully uninstalled scipy-1.4.1\n",
            "  Found existing installation: scikit-learn 0.22.2.post1\n",
            "    Uninstalling scikit-learn-0.22.2.post1:\n",
            "      Successfully uninstalled scikit-learn-0.22.2.post1\n",
            "  Found existing installation: scikit-image 0.16.2\n",
            "    Uninstalling scikit-image-0.16.2:\n",
            "      Successfully uninstalled scikit-image-0.16.2\n",
            "  Found existing installation: torchvision 0.8.1+cu101\n",
            "    Uninstalling torchvision-0.8.1+cu101:\n",
            "      Successfully uninstalled torchvision-0.8.1+cu101\n",
            "  Found existing installation: tqdm 4.41.1\n",
            "    Uninstalling tqdm-4.41.1:\n",
            "      Successfully uninstalled tqdm-4.41.1\n",
            "Successfully installed cffi-1.11.5 coloredlogs-14.0 cython-0.28.3 flask-1.0 gitdb-4.0.5 gitdb2-4.0.2 gitpython-2.1.11 humanfriendly-8.2 matplotlib-2.1.1 numpy-1.14.5 pandas-0.23.1 scikit-image-0.14.0 scikit-learn-0.19.1 scipy-1.1.0 smmap-3.0.4 torchfile-0.1.0 torchvision-0.2.1 tqdm-4.19.5 visdom-0.1.7\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "application/vnd.colab-display-data+json": {
              "pip_warning": {
                "packages": [
                  "cffi",
                  "matplotlib",
                  "mpl_toolkits",
                  "numpy",
                  "pandas"
                ]
              }
            }
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QNEo96LdWxd8"
      },
      "source": [
        "# **Train AlphaZero**"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vsMxLhdKWO-H"
      },
      "source": [
        "import logging\n",
        "import coloredlogs\n",
        "from Coach import Coach\n",
        "from utils import dotdict\n",
        "from dotsandboxes.keras.NNet import NNetWrapper\n",
        "from dotsandboxes.DotsAndBoxesGame import DotsAndBoxesGame"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pJi0kk_djiKm"
      },
      "source": [
        "log = logging.getLogger(__name__)\n",
        "coloredlogs.install(level='INFO')  # Change this to DEBUG to see more info."
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WzIsb1wGVXkE"
      },
      "source": [
        "args = dotdict({\n",
        "    'numIters': 1000,\n",
        "    'numEps': 100,              # Number of complete self-play games to simulate during a new iteration.\n",
        "    'tempThreshold': 15,        #\n",
        "    'updateThreshold': 0.6,     # During arena playoff, new neural net will be accepted if threshold or more of games are won.\n",
        "    'maxlenOfQueue': 200000,    # Number of game examples to train the neural networks.\n",
        "    'numMCTSSims': 25,          # Number of games moves for MCTS to simulate.\n",
        "    'arenaCompare': 40,         # Number of games to play during arena play to determine if new net will be accepted.\n",
        "    'cpuct': 1,\n",
        "    'checkpoint': './temp/',\n",
        "    'load_model': False,\n",
        "    'numItersForTrainExamplesHistory': 20,\n",
        "})"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "W8_4QdwlXZ3k",
        "outputId": "cfb8c1cb-2913-4f5c-cb51-25587937ab93",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "# If you have a pre-trained model, you can load it here.\n",
        "import os\n",
        "if os.path.exists(os.path.join('pretrained_models', 'dotsandboxes', 'keras', '3x3', 'best.pth.tar.index')):\n",
        "  print (\"Using best pre-existing model\")\n",
        "  args['load_model'] = True\n",
        "  args['load_folder_file'] = ('pretrained_models/dotsandboxes/keras/3x3','best.pth.tar')\n",
        "else:\n",
        "  print (\"Not using best pre-existing model\")"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Using best pre-existing model\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_oIZd4tVXGjI"
      },
      "source": [
        "# Set very low iterations to let this notebook run in its entirety.\n",
        "\n",
        "# In reality, training a model, even as simple as the one for Dots and Boxes, can take several hours or days.\n",
        "args['numIters'] = 2\n",
        "args['numEps'] = 2\n",
        "args['arenaCompare'] = 2"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "c5fKMHufYD3m"
      },
      "source": [
        "game = DotsAndBoxesGame(n=3)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VGKmzJ9kYF9O"
      },
      "source": [
        "nnet = NNetWrapper(game)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KvkW0rl9YHeE",
        "outputId": "0bbe1c02-222c-4c29-f88a-88f613a3bbf8",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "if args.load_model:\n",
        "    print('Loading checkpoint \"{}/{}\"...'.format(args.load_folder_file[0], args.load_folder_file[1]))\n",
        "    nnet.load_checkpoint(args.load_folder_file[0], args.load_folder_file[1])\n",
        "else:\n",
        "    print('Not loading a checkpoint.')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Loading checkpoint \"pretrained_models/dotsandboxes/keras/3x3/best.pth.tar\"...\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ESToLzAkYKED"
      },
      "source": [
        "coach = Coach(game, nnet, args)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0uJRc2aRVjN-",
        "outputId": "f89c606e-853f-4a93-c13c-bc109920d981",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "%time coach.learn()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "2020-11-08 02:05:04 dece90a6a033 Coach[66] INFO Starting Iter #1 ...\n",
            "Self Play: 100%|██████████| 2/2 [00:25<00:00, 12.62s/it]\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Checkpoint Directory exists! \n",
            "Epoch 1/10\n",
            "8/8 [==============================] - 0s 4ms/step - loss: 0.5881 - pi_loss: 0.5198 - v_loss: 0.0683\n",
            "Epoch 2/10\n",
            "8/8 [==============================] - 0s 4ms/step - loss: 0.5992 - pi_loss: 0.5420 - v_loss: 0.0572\n",
            "Epoch 3/10\n",
            "8/8 [==============================] - 0s 4ms/step - loss: 0.5989 - pi_loss: 0.5388 - v_loss: 0.0601\n",
            "Epoch 4/10\n",
            "8/8 [==============================] - 0s 4ms/step - loss: 0.5511 - pi_loss: 0.5118 - v_loss: 0.0393\n",
            "Epoch 5/10\n",
            "8/8 [==============================] - 0s 4ms/step - loss: 0.5730 - pi_loss: 0.5315 - v_loss: 0.0415\n",
            "Epoch 6/10\n",
            "8/8 [==============================] - 0s 4ms/step - loss: 0.5990 - pi_loss: 0.5565 - v_loss: 0.0426\n",
            "Epoch 7/10\n",
            "8/8 [==============================] - 0s 4ms/step - loss: 0.5594 - pi_loss: 0.5094 - v_loss: 0.0500\n",
            "Epoch 8/10\n",
            "8/8 [==============================] - 0s 4ms/step - loss: 0.5947 - pi_loss: 0.5444 - v_loss: 0.0503\n",
            "Epoch 9/10\n",
            "8/8 [==============================] - 0s 4ms/step - loss: 0.5410 - pi_loss: 0.5130 - v_loss: 0.0280\n",
            "Epoch 10/10\n",
            "8/8 [==============================] - 0s 4ms/step - loss: 0.5755 - pi_loss: 0.5235 - v_loss: 0.0520\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "2020-11-08 02:05:31 dece90a6a033 Coach[66] INFO PITTING AGAINST PREVIOUS VERSION\n",
            "Arena.playGames (1): 100%|██████████| 1/1 [00:14<00:00, 14.93s/it]\n",
            "Arena.playGames (2): 100%|██████████| 1/1 [00:12<00:00, 13.00s/it]\n",
            "2020-11-08 02:05:59 dece90a6a033 Coach[66] INFO NEW/PREV WINS : 1 / 1 ; DRAWS : 0\n",
            "2020-11-08 02:05:59 dece90a6a033 Coach[66] INFO REJECTING NEW MODEL\n",
            "2020-11-08 02:05:59 dece90a6a033 Coach[66] INFO Starting Iter #2 ...\n",
            "Self Play: 100%|██████████| 2/2 [00:21<00:00, 10.60s/it]\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Checkpoint Directory exists! \n",
            "Epoch 1/10\n",
            "16/16 [==============================] - 0s 4ms/step - loss: 0.5865 - pi_loss: 0.5488 - v_loss: 0.0377\n",
            "Epoch 2/10\n",
            "16/16 [==============================] - 0s 4ms/step - loss: 0.5419 - pi_loss: 0.5215 - v_loss: 0.0205\n",
            "Epoch 3/10\n",
            "16/16 [==============================] - 0s 3ms/step - loss: 0.5158 - pi_loss: 0.5012 - v_loss: 0.0145\n",
            "Epoch 4/10\n",
            "16/16 [==============================] - 0s 3ms/step - loss: 0.5684 - pi_loss: 0.5355 - v_loss: 0.0329\n",
            "Epoch 5/10\n",
            "16/16 [==============================] - 0s 3ms/step - loss: 0.5401 - pi_loss: 0.5246 - v_loss: 0.0155\n",
            "Epoch 6/10\n",
            "16/16 [==============================] - 0s 3ms/step - loss: 0.5431 - pi_loss: 0.5248 - v_loss: 0.0183\n",
            "Epoch 7/10\n",
            "16/16 [==============================] - 0s 4ms/step - loss: 0.5400 - pi_loss: 0.5213 - v_loss: 0.0187\n",
            "Epoch 8/10\n",
            "16/16 [==============================] - 0s 4ms/step - loss: 0.5285 - pi_loss: 0.5120 - v_loss: 0.0165\n",
            "Epoch 9/10\n",
            "16/16 [==============================] - 0s 4ms/step - loss: 0.5316 - pi_loss: 0.5096 - v_loss: 0.0220\n",
            "Epoch 10/10\n",
            "16/16 [==============================] - 0s 4ms/step - loss: 0.5385 - pi_loss: 0.5130 - v_loss: 0.0255\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "2020-11-08 02:06:21 dece90a6a033 Coach[66] INFO PITTING AGAINST PREVIOUS VERSION\n",
            "Arena.playGames (1): 100%|██████████| 1/1 [00:13<00:00, 13.94s/it]\n",
            "Arena.playGames (2): 100%|██████████| 1/1 [00:11<00:00, 11.99s/it]\n",
            "2020-11-08 02:06:47 dece90a6a033 Coach[66] INFO NEW/PREV WINS : 1 / 1 ; DRAWS : 0\n",
            "2020-11-08 02:06:47 dece90a6a033 Coach[66] INFO REJECTING NEW MODEL\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "CPU times: user 1min 39s, sys: 6.36 s, total: 1min 46s\n",
            "Wall time: 1min 43s\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "b4CdwwNWd6vt",
        "outputId": "092f967a-b6eb-475a-eebf-31a54e8d6bd5",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "source": [
        "# Checkpoints and best model (if found) will be saved in this folder\n",
        "%ls /content/alpha-zero-general/temp"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "checkpoint                     temp.pth.tar.data-00000-of-00001\n",
            "checkpoint_0.pth.tar.examples  temp.pth.tar.index\n",
            "checkpoint_1.pth.tar.examples\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}